package com.system.training.entity.dto;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.system.training.entity.Training;


public class TrainingQueryDTO {
	private Long empId;//员工Id
	private String type;//培训类型
	
	public Long getEmpId() {
		return empId;
	}
	
	public String getType() {
		return type;
	}
	
	public void setEmpId(Long empId) {
		this.empId = empId;
	}

	public void setType(String type) {
		this.type = type;
	}
	

	//提供static的工具方法：根据当前 userQueryDTO 对象来组装动态查询条件
	public static Specification<Training> getSpecification(TrainingQueryDTO trainingQueryDTO)
	{
		Specification<Training> spec = new Specification<Training>() {
			public Predicate toPredicate(Root<Training> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != trainingQueryDTO && !StringUtils.isEmpty(trainingQueryDTO.getEmpId())) {
					 Predicate  p1 =  cb.like(root.get("empId").as(String.class),"%"+ trainingQueryDTO.getEmpId() + "%");
					 list.add(p1);
				 }
				 if(null != trainingQueryDTO && !StringUtils.isEmpty(trainingQueryDTO.getType())) {
					 Predicate  p2 =  cb.like(root.get("type").as(String.class),"%"+ trainingQueryDTO.getType() + "%");
					 list.add(p2);
				 }
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}



}
